using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace DiscGolfScore
{
	/// <summary>
	/// Summary description for Results.
	/// </summary>
	public class ResultsForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel pnlResults;
		private System.Windows.Forms.Button btnSave;
		private Course course;
		private System.Windows.Forms.SaveFileDialog saveFileDialog;
		private System.Windows.Forms.Button btnPost;
		private System.Windows.Forms.Label lblStatus;
		private ArrayList players;
	
		public ResultsForm( ArrayList players, Course course )
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			btnSave.BackColor = System.Drawing.Color.White;
			btnPost.BackColor = System.Drawing.Color.White;

			this.players = players;
			this.course = course;
			int count = 0;
			foreach (Player p in players) 
			{
				Label name = new Label();
				name.Text = p.FirstName+" "+p.LastName.Substring(0,1)+". : Total: "+getPlayerScore(p)+" ("+(getPlayerScore(p)-course.GetPar()+")");
				name.Left = 10;
				name.Top = 10 + 30*count;
				name.Width = 200;
				name.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
				pnlResults.Controls.Add(name);
				count++;
			}
		}

		private int getPlayerScore(Player player) 
		{
			int score = 0;
			foreach( Hole h in player.holesPlaying )
			{
				score += h.par;
			}
			return score;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pnlResults = new System.Windows.Forms.Panel();
			this.btnSave = new System.Windows.Forms.Button();
			this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			this.btnPost = new System.Windows.Forms.Button();
			this.lblStatus = new System.Windows.Forms.Label();
			// 
			// pnlResults
			// 
			this.pnlResults.BackColor = System.Drawing.Color.LimeGreen;
			this.pnlResults.Location = new System.Drawing.Point(8, 24);
			this.pnlResults.Size = new System.Drawing.Size(224, 176);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(160, 240);
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// saveFileDialog
			// 
			this.saveFileDialog.FileName = "game.xml";
			this.saveFileDialog.Filter = "XML files|*.xml";
			// 
			// btnPost
			// 
			this.btnPost.Location = new System.Drawing.Point(72, 240);
			this.btnPost.Size = new System.Drawing.Size(80, 20);
			this.btnPost.Text = "Post to web";
			this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
			// 
			// lblStatus
			// 
			this.lblStatus.Location = new System.Drawing.Point(8, 208);
			this.lblStatus.Size = new System.Drawing.Size(224, 32);
			// 
			// ResultsForm
			// 
			this.BackColor = System.Drawing.Color.LawnGreen;
			this.ClientSize = new System.Drawing.Size(240, 272);
			this.Controls.Add(this.lblStatus);
			this.Controls.Add(this.btnPost);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.pnlResults);
			this.Text = "Results";

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			saveFileDialog.FileName = course.Name+"_"+DateTime.Now.ToShortDateString()+".xml";
			saveFileDialog.FileName.Replace("/", "-");	//make sure no invalid characters in filename suggestion
			DialogResult res = saveFileDialog.ShowDialog();

			if( res == DialogResult.OK ) 
			{
				string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
				xml += "<game course=\""+course.Id+"\">\n";
				foreach( Player p in players )
				{
					xml += p.ToXml();
				}		
				xml += "\n</game>";

				StreamWriter f = File.CreateText(saveFileDialog.FileName);
				f.Write(xml);
				f.Close();
				lblStatus.Text = "Results written to "+saveFileDialog.FileName;
			}
		}

		private void btnPost_Click(object sender, System.EventArgs e)
		{
			//copy players ids to an int array for web service method
			int[] player_ids = new int[players.Count];
			for(int i=0; i< player_ids.Length; i++)
			{
				player_ids[i] = ((Player)players[i]).Id;
			}

			//copy scores to an int array for web service method
			int[] scores = new int[player_ids.Length*course.holes.Count];
			int count = 0;
			foreach( Player p in players ) 
			{
				foreach( Hole h in p.holesPlaying ) 
				{
					scores[count] = h.par;
					count++;
				}
			}

			//post the result to webpage
			try 
			{
				WebReference1.FrisbeeGameService webservice = new DiscGolfScore.WebReference1.FrisbeeGameService();
				int reply = webservice.registerScore(course.Id, player_ids, scores);
				lblStatus.Text = reply.ToString();
			}
			catch( System.Net.WebException ex ) 
			{
				lblStatus.Text = ex.Message;
			}
			catch( System.Web.Services.Protocols.SoapException ex ) 
			{
				lblStatus.Text = ex.Message;
			}
			
		
		}
	}
}
